package com.codeest.geeknews.base;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;

/**
 * Created by admin on 2017/7/12.
 */

public abstract class BaseCircularActivity extends SimpleActivity {
    private View content;
    private int mX;
    private int mY;
    private int mFromColor;
    private int mToColor;
    private int argbEvaluatorViewIds;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(getLayout());
        content = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
        mX = getIntent().getIntExtra("x", 0);
        mY = getIntent().getIntExtra("y", 0);
        mFromColor = getIntent().getIntExtra("fromColor", -1);
        mToColor = getIntent().getIntExtra("toColor", -1);
        argbEvaluatorViewIds = getIntent().getIntExtra("argbEvaluatorViewId", -1);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            content.post(new Runnable() {
                @Override
                public void run() {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                        Animator animator;
                        if (mFromColor != -1) {
                            animator = createRevealAnimatorSet(false, false);
                        } else {
                            animator = createRevealAnimator(false);
                        }
                        animator.start();
                    }
                }
            });
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private Animator createRevealAnimator(boolean reversed) {
        float hypot = (float) Math.hypot(content.getHeight(), content.getWidth());
        float startRadius = reversed ? hypot : 0;
        float endRadius = reversed ? 0 : hypot;

        Animator animator = ViewAnimationUtils.createCircularReveal(
                content, mX, mY,
                startRadius,
                endRadius);
        animator.setDuration(800);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        if (reversed)
            animator.addListener(animatorListener);
        return animator;
    }

    /**
     * @param reversed      进入动画
     * @param colorReversed false 颜色进入动画,
     *                      true 颜色退出动画
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private AnimatorSet createRevealAnimatorSet(boolean reversed, boolean colorReversed) {
        float hypot = (float) Math.hypot(content.getHeight(), content.getWidth());
        float startRadius = reversed ? hypot : 0;
        float endRadius = reversed ? 0 : hypot;

        AnimatorSet animatorSet = new AnimatorSet();
        Animator animator = ViewAnimationUtils.createCircularReveal(
                content, mX, mY,
                startRadius,
                endRadius);
        animator.setDuration(800);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        if (reversed)
            animator.addListener(animatorListener);
        //
        if (argbEvaluatorViewIds != -1) {
            ObjectAnimator backgroundColorAnimator;
            View argbEvaluatorView = findViewById(argbEvaluatorViewIds);
            if (!colorReversed) {
                backgroundColorAnimator = ObjectAnimator.ofInt(argbEvaluatorView, "backgroundColor", mFromColor, mToColor);
            } else {
                backgroundColorAnimator = ObjectAnimator.ofInt(argbEvaluatorView, "backgroundColor", mToColor, mFromColor);
            }
            backgroundColorAnimator.setDuration(800);
            backgroundColorAnimator.setEvaluator(new ArgbEvaluator());
            backgroundColorAnimator.start();
            animatorSet.playTogether(animator, backgroundColorAnimator);
        } else {
            animatorSet.playTogether(animator);
        }
        //
        return animatorSet;
    }

    private Animator.AnimatorListener animatorListener = new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            content.setVisibility(View.INVISIBLE);
            finish();
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }
    };

    @Override
    //public void onBackPressed() {
    public void onBackPressedSupport() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //Animator animator = createRevealAnimator(true, mX, mY);
            Animator animator;
            if (mFromColor != -1) {
                animator = createRevealAnimatorSet(true, true);
            } else {
                animator = createRevealAnimator(true);
            }
            animator.start();
        } else {
            finish();
        }
    }
}
